$(function () {
    function getCommentList() {
        $.ajax({
            method: 'get',
            url: 'http://www.liulongbin.top:3006/api/cmtlist',
            data:[],
            success: function (res) {
                if(res.status !== 200) return alert('请求列表失败');
                var rows = [];
                $.each(res.data,function (i,item) {
                    var str = '<li class="list-group-item"><span class="badge" style="background-color: #f0ad4e;">评论时间: '+item.time+'</span><span class="badge" style="background-color: #5bc0de;">评论人: '+item.username+'</span>'+item.content+'</li>';
                    rows.push(str);
                })
                $('#list-group').empty().append(rows.join(''));
            }
        })
    }
    getCommentList();
    $('#panel-body').submit(function (e) {
        e.preventDefault();
        var data = $(this).serialize();
        $.ajax({
            method:'post',
            url:'http://www.liulongbin.top:3006/api/addcmt',
            data:data,
            success: function (res) {
                if(res.status !== 201) return alert('发送数据失败');
                console.log(res);
                getCommentList();
                $('#panel-body')[0].reset();
            }
        })
    })
})